
import traceback
import logging

from django.shortcuts import render

# Create your views here.
from django.db.models import Q
from rest_framework import permissions
from rest_framework.response import Response
from rest_framework.decorators import action
from rest_framework import generics, mixins, views
from django_nameko import get_pool
from drf_yasg2.utils import swagger_auto_schema
from drf_yasg2 import openapi
from account.models import Account
from admin_manage.serializers import AccountManageSerializer, AccountRoleInfoSerializer, RoleInfoSerializer

# Create your views here.


from account.models import Role, Resource
from common.utils import LargeResultsSetPagination
from common.custom_permission import IsPointsManagerPermission


class AddRole(generics.GenericAPIView):
    """
    添加角色数据
    """
    pass


class GetAllRole(generics.GenericAPIView):
    """
    获取所有的角色数据
    """
    serializer_class = RoleInfoSerializer
    # TODO 增加自定义权限
    permission_classes = [permissions.IsAuthenticated, IsPointsManagerPermission]

    query_param = []

    @swagger_auto_schema(method='get', manual_parameters=query_param)
    @action(methods=['get'], detail=False)
    def get(self, request):
        role_list = Role.objects.all()
        data = self.get_serializer(role_list, many=True).data
        return Response({'code': 200, 'data': data})


class GetUserRoleInfo(generics.GenericAPIView):
    """
    获取指定用户的权限数据
    """
    serializer_class = AccountRoleInfoSerializer
    # TODO 增加自定义权限
    permission_classes = [permissions.IsAuthenticated, ]

    query_param = [openapi.Parameter(name='user_id', in_=openapi.IN_QUERY, description="用户id", type=openapi.TYPE_STRING),
                   ]

    @swagger_auto_schema(method='get', manual_parameters=query_param)
    @action(methods=['get'], detail=False)
    def get(self, request):
        data = request.query_params
        user_id = data.get('user_id')
        user = Account.objects.get(id=user_id)
        data = self.get_serializer(user).data
        return Response({'code': 200, 'data': data})


class SearchUserList(generics.GenericAPIView):
    """
    获取用户列表  可以按名字查询
    """
    serializer_class = AccountManageSerializer
    permission_classes = [permissions.IsAuthenticated]

    query_param = [openapi.Parameter(name='q', in_=openapi.IN_QUERY, description="查询条件", type=openapi.TYPE_STRING),
                   openapi.Parameter(name='size', in_=openapi.IN_QUERY, description="页数大小", type=openapi.TYPE_NUMBER),
                   openapi.Parameter(name='page', in_=openapi.IN_QUERY, description="页码", type=openapi.TYPE_NUMBER),
                   ]

    @swagger_auto_schema(method='get', manual_parameters=query_param)
    @action(methods=['get'], detail=False)
    def get(self, request):
        data = request.query_params
        q = data.get('q')
        if not q:
            result_list = Account.objects.all().order_by('-id')
        else:
            result_list = Account.objects.filter(username__icontains=q).order_by('-id')

        total_count = result_list.count()

        # 实例化分页对象
        page_cursor = LargeResultsSetPagination()
        # 分页
        data_list = page_cursor.paginate_queryset(result_list, request, view=self)

        data_list = self.get_serializer(data_list, many=True).data
        result = {'code': 200, 'data': data_list, 'total_count': total_count}
        return Response(result)
